/**
 * 微信-阅读量/点赞量
 */
(function ($, window, document, undefined) {
    var comChart;

    var WechatPlaylike = function (el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption: {
                title: {
                    text: '阅读量/点赞量',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['阅读量', '点赞量']
                },
                calculable: true,
                dataZoom: {
                    show: true,
                    realtime: true,
                    start: 0,
                    end: 100
                },
                xAxis: [
                    {
                        type: 'category',
                        data: []
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '阅读量',
                        type: 'bar',
                        data: [],
                        markPoint: {
                            data: [
                                {
                                    type: 'max',
                                    name: '最大值'
                                },
                                {
                                    type: 'min',
                                    name: '最小值'
                                }
                            ]
                        },
                        markLine: {
                            data: [
                                {
                                    type: 'average',
                                    name: '平均值'
                                }
                            ]
                        }
                    },
                    {
                        name: '点赞量',
                        type: 'bar',
                        data: [],
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {
                                    type: 'average',
                                    name: '平均值'
                                }
                            ]
                        }
                    }
                ]
            }
        };
        this.options = $.extend({}, this.defaults, opts)
    };

    WechatPlaylike.prototype = {
        init: function () {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == true) {
                return me;
            }

            me.initChart();
            me.bindEvents();

            el.attr('data-initialized', 'true');
            return me;
        },

        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function () {
            var me = this,
                el = me.el;
        },

        formatData: function (originData) {
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption,
                keys = ['read_num', 'like_num'],
                i, j, len1, len2;

            //初始化xaxis
            for (var i = 0, len1 = originData.length; i < len1; i++) {
                chartOption.xAxis[0].data.push(originData[i]['date']);
            }

            // 初始化series
            $.each(keys, function (i, key) {
                chartOption.series[i].data = [];
            });
            for (i = 0, len1 = originData.length; i < len1; i++) {
                for (j = 0, len2 = keys.length; j < len2; j++) {
                    chartOption.series[j].data.push(originData[i][keys[j]]);
                }
            }

            return chartOption;


        },

        initChart: function () {
            var me = this,
                el = me.el,
                option = {};

            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);
//            $.ajax({
//                url: $.fn.utils.env == 'dev' ? '/keywords/1/wechat_playlike.json' : '',
//                type: 'GET',
//                dataType: 'json',
//                data: {},
//                success: function (ret) {
//                    if (ret && ret.status) {
//                        var data = ret.data[0].wechat_playlike;
//
//                        option = me.formatData(data);
//                        comChart.setOption(option);
//                    } else {
//
//                    }
//                },
//                error: function (ret) {
//
//                }
//            });
        }
    };

    $.fn.WechatPlaylike = function (opts) {
        var me = this,
            el = me.el,
            comWechatPlaylike;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comWechatPlaylike = new WechatPlaylike(this, opts);
        }

        return comWechatPlaylike.init();
    };


})(jQuery, window, document);


